import * as mobx from 'mobx';
import {observable, action, useStrict, computed, asMap} from 'mobx';
import {getEcCom} from "../util/convert";

const {WeaTools} = getEcCom();

const ls = WeaTools.ls;

export default class SecondAuthStore {
    /**
     * 二次验证成功后，确认时，调用的流程提交方法
     */
    callbackFunc = null;

    /**
     *  流程提交时的参数
     */
    callbackParam = {};

    /**
     * 二次验证窗口需要的一些参数，比如authType等
     */
    params = {};

    /**
     * 是否需要二次验证
     */
    isEnableAuth = 0;

    /**
     * 二次验证的类型
     */
    secondAuthType = 0;

    /**
     *  是否启用了数据保护
     */
    isEnableProtect = 0;

    /**
     *  数据保护的类型
     */
    protectType = 0;

    /**
     * 二次验证成功
     */
    isAuthSuccess = 0;

    /**
     * 保护数据成功
     */
    isProtectSuccess = 0;

    /**
     *  0：表示二次验证的窗口
     *  1：表示是CA数据保护
     *  10：表示是CA认证和数据保护一起弹出的窗口
     */
    isProtectDialog = 0;

    /**
     * 是否是提交到退回节点
     */
    isSubmitDirect = 0;

    constructor() {
        this.setCallbackFunc = this.setCallbackFunc;
        this.setCallbackParam = this.setCallbackParam;
        this.setParams = this.setParams;
        this.setIsSubmitDirect = this.setIsSubmitDirect;
        this.setDialogData = this.setDialogData;
        this.setDialogResult = this.setDialogResult;
        this.saveDataToStorage = this.saveDataToStorage;
        this.readDataFromStorage = this.readDataFromStorage;
        this.resetStore = this.resetStore;
        this.callbackFunc = () => {
        }
        this.callbackParam = {};
    }

    @action
    setCallbackFunc = (func)  =>  {
        this.callbackFunc = func;
    }

    /**
     *  设置二次验证后提交流程时的参数
     *  这个是从流程提交时的方法中设置的
     */
    @action("验证成功后提交流程的参数")
    setCallbackParam = (param) =>  {
        this.callbackParam = {...this.callbackParam, ...param};
    }

    @action
    setParams = (paras)  =>  {
        this.params = paras;
    }

    setIsSubmitDirect = (val = 0)  => {
        this.isSubmitDirect = val;
    }

    @action("验证或者数据保护的窗口需要的数据")
    setDialogData = (data) =>  {
        /*
        this.isEnableAuth = data.isEnableAuth || this.isEnableAuth;
         this.secondAuthType = data.secondAuthType || this.secondAuthType;
         this.isEnableProtect = data.isEnableProtect || this.isEnableProtect;
         this.protectType = data.protectType || this.protectType;
         */

        /**
         * isEnableAuth, secondAuthType, isEnableProtect, protectType参数的值
         */
        Object.assign(this, data);
    }

    @action("设置CA认证或者保护时的数据")
    setCAAuthData  = (data) =>  {
        /**
         * 设置isProtectDialog,authKey,sourcestr参数的值
         */
        const isProtectDialog = data.isProtectDialog;
        let sourcestr = data.sourcestr;
        if (isProtectDialog == 0 && !sourcestr) {
            sourcestr = Math.random();
            data.sourcestr = sourcestr;
        }
        Object.assign(this, data);
    }

    @action("设置验证或者数据保护的结果")
    setDialogResult = (data)  => {
        /*
        this.isAuthSuccess = data.isAuthSuccess || this.isAuthSuccess;
         this.isProtectSuccess = data.isProtectSuccess || this.isProtectSuccess;
         */

        /**
         * 设置isAuthSuccess,isProtectSuccess参数的值
         */
        Object.assign(this, data);
    }

    @action("将数据存入localStorage")
    saveDataToStorage = (authKey)  => {
        const callbackParam = this.callbackParam;
        const isEnableAuth = this.isEnableAuth;
        const secondAuthType = this.secondAuthType;
        const isEnableProtect = this.isEnableProtect;
        const protectType = this.protectType;
        const isAuthSuccess = this.isAuthSuccess;
        const isProtectSuccess = this.isProtectSuccess;
        const isProtectDialog = this.isProtectDialog;
        const isSubmitDirect = this.isSubmitDirect;
        const obj = {
            callbackParam,
            isEnableAuth,
            secondAuthType,
            isEnableProtect,
            protectType,
            isAuthSuccess,
            isProtectSuccess,
            isProtectDialog,
            isSubmitDirect
        };

        ls.set(authKey, JSON.stringify(obj));
    }

    @action("从localStorage中读取数据")
    readDataFromStorage = (authKey)  => {
        const obj = ls.getJSONObj(authKey);
        const {
            callbackParam,
            isEnableAuth,
            secondAuthType,
            isEnableProtect,
            protectType,
            isAuthSuccess,
            isProtectSuccess,
            isProtectDialog,
            isSubmitDirect
        } = obj;

        this.callbackParam = callbackParam;
        this.isEnableAuth = isEnableAuth;
        this.secondAuthType = secondAuthType;
        this.isEnableProtect = isEnableProtect;
        this.protectType = protectType;
        this.isAuthSuccess = isAuthSuccess;
        this.isProtectSuccess = isProtectSuccess;
        this.isProtectDialog = isProtectDialog;
        this.isSubmitDirect = isSubmitDirect;
    }

    @action("重置所有值为初始化")
    resetStore = ()  =>  {
        this.callbackFunc = () => {
        };
        this.callbackParam = {};
        this.isEnableAuth = 0;
        this.secondAuthType = 0;
        this.isEnableProtect = 0;
        this.protectType = 0;
        this.isAuthSuccess = 0;
        this.isProtectSuccess = 0;
        this.isProtectDialog = 0;
        this.isSubmitDirect = 0;
    }
}